home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Delphi Informant Complete 1995 - 2000
/
Delphi Informant Complete 1995 to 2000.iso
/
Delphi Informant Magazine Complete Works SOURCE CODE 1998.rar
/
1998
/
Sep
/
di9809cd
/
WPTelevision.DPR
< prev
next >
Wrap
Text File
|
1997-12-23
|
15KB
|
476 lines
Library WPTelevision;
uses
SysUtils,
Windows,
Registry,
ShellAPI,
sharedmemory in 'SharedMemory.pas',
WinPerf in 'WinPerf.pas',
TelevisionLIB in 'TelevisionLIB.pas';
//
// SMARTBroker data structures
//
Const
OpenCount : Integer = 0; // count of "Open" threads
NumberOfCounters : Integer = 6;
DefaultCounter : Integer = 2;
DLLInitOK : Boolean = False; // true = DLL initialized OK
ObjectCounter = Television;
ObjectName : String = 'Television';
SharedMemName : String = 'Television_SMEM';
DLLName : String = 'WPTelevision.DLL';
ININame : String = 'WPTelevision.INI';
FirstActualCounter = TelevisionPower;
//
// The OUTPUT_COUNTERS structure gives the layout of counters in
// the performance output structure.
//
type
PPointer = ^Pointer;
POUTPUT_COUNTERS = ^TOUTPUT_COUNTERS;
TOUTPUT_COUNTERS = record
Power : LongInt;
Channel : LongInt;
ClosedCaptioningEnabled : LongInt;
SleepTimer : LongInt;
TimeOn : LongInt;
ClosedCaptioningWordsPerSecond : LongInt;
end;
var
FirstCounter : Cardinal; // The first counter offset from the registry
FirstHelp : Cardinal; // The first help offset from the registry.
SharedMem : TSharedMem;
ACCESS_SMEM : PTELEVISION_SMEM;
pOutputStructure : LPDWORD;
function GetSizeOfSpecificCounter(CounterNum : Integer) : Integer;
begin
Result:=0;
case CounterNum of
0 : Result:=SizeOf(DWORD);
1 : Result:=SizeOf(DWORD);
2 : Result:=SizeOf(DWORD);
3 : Result:=SizeOf(DWORD);
4 : Result:=SizeOf(DWORD);
5 : Result:=SizeOf(DWORD);
end;
end;
function GetSizeOfAllCounters : Integer;
var
i : Integer;
begin
Result:=0;
for i:=0 to NumberOfCounters-1 Do Result:=Result+GetSizeofSpecificCounter(i);
end;
function OpenData(lpDeviceNames : PWideChar) : DWord; stdcall;
var
Registry : TRegistry;
keystr : String;
begin
/////////////////////////////////////////////////////////
//Routine Description:
//
// This routine will open and map the memory used by the SMART object to
// pass performance data.
//
//Arguments:
//
// Pointer to object ID of each device to be opened.
//
//
//Return Value:
//
// None.
/////////////////////////////////////////////////////////
//
// Since SCREG is multi-threaded and will call this routine in
// order to service remote performance queries, this library
// must keep track of how many times it has been opened (i.e.
// how many threads have accessed it). the registry routines will
// limit access to the initialization routine to only one thread
// at a time so synchronization (i.e. reentrancy) should not be
// a problem
//
if (OpenCount=0) then
begin
// open shared memory used by object to pass performance values
SharedMem:=TSharedMem.Access(SharedMemName);
if (SharedMem.Buffer<>nil) then
begin
ACCESS_SMEM := SharedMem.Buffer;
// get counter and help index base values from registry
// Open key to registry entry
// read First Counter and First Help values
Registry:=TRegistry.Create;
Registry.RootKey:=HKEY_LOCAL_MACHINE;
keystr:='SYSTEM\CurrentControlSet\Services\'+ObjectName+'\Performance';
if (not Registry.OpenKey(keystr,False)) then
begin
// this is fatal, if we can't get the base values of the
// counter or help names, then the names won't be available
// to the requesting application so there's not much
// point in continuing.
Result:=GetLastError;
Registry.CloseKey;
Exit;
end;
if (not Registry.ValueExists('First Counter')) then
begin
// this is fatal, if we can't get the base values of the
// counter or help names, then the names won't be available
// to the requesting application so there's not much
// point in continuing.
Result:=GetLastError;
Registry.CloseKey;
Exit;
end else
FirstCounter:=Registry.ReadInteger('First Counter');
if (not Registry.ValueExists('First Help')) then
begin
// this is fatal, if we can't get the base values of the
// counter or help names, then the names won't be available
// to the requesting application so there's not much
// point in continuing.
Result:=GetLastError;
Registry.CloseKey;
Exit;
end else
FirstHelp:=Registry.ReadInteger('First Help');
Registry.CloseKey;
DLLInitOK:=True;
Registry.Free;
end else
begin
Result:=GetLastError;
Exit;
end;
end;
Inc(OpenCount);
Result:=ERROR_SUCCESS;
end;
function CloseData : DWord; stdcall;
begin
////////////////////////////////////////////////////////
//
//Routine Description:
//
// This routine closes the open handles to SMART
// device performance counters
//
//Arguments:
//
// None.
//
//Return Value:
//
// ERROR_SUCCESS
//
/////////////////////////////////////////////////////
Dec(OpenCount);
if (OpenCount=0) then
begin
SharedMem.Free;
if (pOutputStructure^<>0) then
begin
FreeMem(pOutputStructure,SizeOf(Integer));
pOutputStructure:=nil;
end;
SharedMem:=nil;
end;
Result:=ERROR_SUCCESS;
end;
(************************************************************************)
procedure InitializeOutputStructure;
var
SpaceNeeded : Cardinal;
i,j,total : Integer;
pOutput : LPDWORD;
begin
// Allocate and initialize the response buffer.
if (pOutputStructure<>nil)then Exit; //nothing to do
SpaceNeeded:=sizeof(TPERF_OBJECT_TYPE) + sizeof(TPERF_COUNTER_BLOCK);
for i:=0 to NumberOfCounters-1 do
SpaceNeeded:=SpaceNeeded +
sizeof(TPERF_COUNTER_DEFINITION) +
GetSizeOfSpecificCounter(i);
GetMem(pOutputStructure,SpaceNeeded);
pOutput:=pOutputStructure;
//
// The following lines initialize the PERF_OBJECT_TYPE structure.
//
// Set the index to the next object
pOutput^:=DWORD(SpaceNeeded);
Inc(pOutput);
// Set the index to the first counter block.
pOutput^ := DWORD(SpaceNeeded-SizeOf(TPERF_COUNTER_BLOCK)-GetSizeOfAllCounters);
Inc(pOutput);
// Set index to first counter
pOutput^:=DWORD(SizeOf(TPERF_OBJECT_TYPE));
Inc(pOutput);
// Set object name index.
pOutput^:=DWORD(FirstCounter+ObjectCounter);
Inc(pOutput);
// Null out the object name title
pOutput^:=DWord(0);
Inc(pOutput);
// Set object help index.
pOutput^:=DWORD(FirstHelp+ObjectCounter);
Inc(pOutput);
// Null out the object help title
pOutput^:=DWord(0);
Inc(pOutput);
// Set the detail level
pOutput^:=DWORD(PERF_DETAIL_ADVANCED);
Inc(pOutput);
// Set the number of counters.
pOutput^:=DWORD(NumberOfCounters);
Inc(pOutput);
// Set the default counter
pOutput^:=DWORD(DefaultCounter);
Inc(pOutput);
// Set no instances.
pOutput^:=DWORD(PERF_NO_INSTANCES);
Inc(pOutput);
// Indicate the code page; in this case Unicode strings
pOutput^:=DWORD(0);
Inc(pOutput);
// Pad out PerfTime
pOutput^:=DWORD(0);
Inc(pOutput);
pOutput^:=DWORD(0);
Inc(pOutput);
// Pad out PerfFreq
pOutput^:=DWORD(0);
Inc(pOutput);
pOutput^:=DWORD(0);
Inc(pOutput);
//
// The following loop initializes the PERF_COUNTER_DEFINITION structures.
//
for i:=0 to NumberOfCounters-1 do
begin
// Set index to next counter definition.
pOutput^:=DWORD(SizeOf(TPERF_COUNTER_DEFINITION));
Inc(pOutput);
// Set counter name index
pOutput^:=DWORD(FirstActualCounter + FirstCounter + i + i);
Inc(pOutput);
// Null out counter name title
pOutput^:=DWord(0);
Inc(pOutput);
// Set counter help index
pOutput^:=DWORD(FirstActualCounter + FirstHelp + i + i);
Inc(pOutput);
// Null out counter help title
pOutput^:=DWord(0);
Inc(pOutput);
//